﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace SharepointListFieldsMassiveCreator
{
    public class FieldSchemaGenerationHelper
    {
        public static String GetFieldDefinition(String fieldName, String fieldDefinitionSchema)
        {
            String fieldDefinitionResult = "";
            XElement schemaAnalysis = XElement.Parse(fieldDefinitionSchema);

            if (schemaAnalysis.Element("Type").Attribute("choiceType") != null)
            {
                if (schemaAnalysis.Element("Type").Value.ToLower() == "choice" &&
                    schemaAnalysis.Element("Type").Attribute("choiceType").Value.ToLower() == "multi")
                    fieldDefinitionResult = GetChoiceCheckBoxesFieldSchema(fieldName, fieldDefinitionSchema);
                if (schemaAnalysis.Element("Type").Value.ToLower() == "choice" &&
                    (schemaAnalysis.Element("Type").Attribute("choiceType").Value.ToLower() == "radio"))
                    fieldDefinitionResult = GetChoiceRadioButtonsFieldSchema(fieldName, fieldDefinitionSchema);
                if (schemaAnalysis.Element("Type").Value.ToLower() == "choice" &&
                    (schemaAnalysis.Element("Type").Attribute("choiceType") == null &&
                     schemaAnalysis.Element("Type").Attribute("choiceType").Value.ToLower() == "dropdown"))
                    fieldDefinitionResult = GetChoiceDropDownListFieldSchema(fieldName, fieldDefinitionSchema);
            }
            else
            {
                if (schemaAnalysis.Element("Type").Value.ToLower() == "choice")
                    fieldDefinitionResult = GetChoiceDropDownListFieldSchema(fieldName, fieldDefinitionSchema);
            }

            if (schemaAnalysis.Element("Type").Value.ToLower() == "boolean")
                fieldDefinitionResult = GetBooleanFieldSchema(fieldName, fieldDefinitionSchema);

            if (schemaAnalysis.Element("Type").Value.ToLower() == "date")
                fieldDefinitionResult = GetDateFieldSchema(fieldName, fieldDefinitionSchema);

            if (schemaAnalysis.Element("Type").Value.ToLower() == "text")
                fieldDefinitionResult = GetTextFieldSchema(fieldName, fieldDefinitionSchema);

            if (schemaAnalysis.Element("Type").Value.ToLower() == "multiline")
                fieldDefinitionResult = GetMultiLineFieldSchema(fieldName, fieldDefinitionSchema);

            if (schemaAnalysis.Element("Type").Value.ToLower() == "number")
                fieldDefinitionResult = GetNumberFieldSchema(fieldName, fieldDefinitionSchema);

            if (schemaAnalysis.Element("Type").Value.ToLower() == "currency")
                fieldDefinitionResult = GetCurrencyFieldSchema(fieldName, fieldDefinitionSchema);

            if (schemaAnalysis.Element("Type").Value.ToLower() == "calculated")
                fieldDefinitionResult = GetCalculatedFieldSchema(fieldName, fieldDefinitionSchema);

            if (schemaAnalysis.Element("Type").Value.ToLower() == "user")
                fieldDefinitionResult = GetUserFieldSchema(fieldName, fieldDefinitionSchema);

            if (schemaAnalysis.Element("Type").Value.ToLower() == "usermulti")
                fieldDefinitionResult = GetUserMultiFieldSchema(fieldName, fieldDefinitionSchema);

            if (schemaAnalysis.Element("Type").Value.ToLower() == "link")
                fieldDefinitionResult = GetLinkFieldSchema(fieldName, fieldDefinitionSchema);

            if (schemaAnalysis.Element("Type").Value.ToLower() == "picture")
                fieldDefinitionResult = GetPictureFieldSchema(fieldName, fieldDefinitionSchema);

            // Integer numbers are not supported column data types in SharePoint and InfoPath. Use Number with Decimals="0" instead.
            if (schemaAnalysis.Element("Type").Value.ToLower() == "integer")
                fieldDefinitionResult = GetIntegerFieldSchema(fieldName, fieldDefinitionSchema);

            schemaAnalysis = null;
            return fieldDefinitionResult;
        }

        private static string GetBooleanFieldSchema(string fieldName, string fieldDefinitionSchema)
        {
            XElement schemaAnalysis = XElement.Parse(fieldDefinitionSchema);
            bool isHidden = ((schemaAnalysis.Element("Type").Attribute("Hidden") != null) ? true : false);
            return String.Format(SharepointListFieldsMassiveCreator.Properties.Resources.BooleanFieldBody,
                                 fieldName, isHidden.ToString().ToUpper());
        }

        private static string GetUserFieldSchema(string fieldName, string fieldDefinitionSchema)
        {
            XElement schemaAnalysis = XElement.Parse(fieldDefinitionSchema);
            bool isHidden = ((schemaAnalysis.Element("Type").Attribute("Hidden") != null) ? true : false);
            return String.Format(SharepointListFieldsMassiveCreator.Properties.Resources.UserFieldBody,
                                 fieldName, isHidden.ToString().ToUpper());
        }

        private static string GetUserMultiFieldSchema(string fieldName, string fieldDefinitionSchema)
        {
            XElement schemaAnalysis = XElement.Parse(fieldDefinitionSchema);
            bool isHidden = ((schemaAnalysis.Element("Type").Attribute("Hidden") != null) ? true : false);
            return String.Format(SharepointListFieldsMassiveCreator.Properties.Resources.UserMultiFieldBody,
                                 fieldName, isHidden.ToString().ToUpper());
        }

        private static string GetChoiceDropDownListFieldSchema(string fieldName, string fieldDefinitionSchema)
        {
            XElement schemaAnalysis = XElement.Parse(fieldDefinitionSchema);
            bool isHidden = ((schemaAnalysis.Element("Type").Attribute("Hidden") != null) ? true : false);
            String choiceOptions = schemaAnalysis.Element("CHOICES").ToString();
            return String.Format(SharepointListFieldsMassiveCreator.Properties.Resources.ChoiceDropDownFieldBody,
                                 fieldName, isHidden.ToString().ToUpper(), choiceOptions);
        }

        private static string GetChoiceCheckBoxesFieldSchema(string fieldName, string fieldDefinitionSchema)
        {
            XElement schemaAnalysis = XElement.Parse(fieldDefinitionSchema);
            bool isHidden = ((schemaAnalysis.Element("Type").Attribute("Hidden") != null) ? true : false);
            String choiceOptions = schemaAnalysis.Element("CHOICES").ToString();
            return String.Format(SharepointListFieldsMassiveCreator.Properties.Resources.MultiChoiceFieldBody,
                                 fieldName, isHidden.ToString().ToUpper(), choiceOptions);
        }

        private static string GetChoiceRadioButtonsFieldSchema(string fieldName, string fieldDefinitionSchema)
        {
            XElement schemaAnalysis = XElement.Parse(fieldDefinitionSchema);
            bool isHidden = ((schemaAnalysis.Element("Type").Attribute("Hidden") != null) ? true : false);
            String choiceOptions = schemaAnalysis.Element("CHOICES").ToString();
            return String.Format(SharepointListFieldsMassiveCreator.Properties.Resources.ChoiceRadioButtonsFieldBody,
                                 fieldName, isHidden.ToString().ToUpper(), choiceOptions);
        }

        private static string GetMultiLineFieldSchema(string fieldName, string fieldDefinitionSchema)
        {
            XElement schemaAnalysis = XElement.Parse(fieldDefinitionSchema);
            bool isHidden = ((schemaAnalysis.Element("Type").Attribute("Hidden") != null) ? true : false);
            return String.Format(SharepointListFieldsMassiveCreator.Properties.Resources.MultiLineTextFieldBody,
                                 fieldName, isHidden.ToString().ToUpper());
        }

        private static string GetDateFieldSchema(string fieldName, string fieldDefinitionSchema)
        {
            XElement schemaAnalysis = XElement.Parse(fieldDefinitionSchema);
            bool isHidden = ((schemaAnalysis.Element("Type").Attribute("Hidden") != null) ? true : false);
            return String.Format(SharepointListFieldsMassiveCreator.Properties.Resources.DateTimeFieldBody,
                                 fieldName, isHidden.ToString().ToUpper());
        }

        private static String GetTextFieldSchema(String fieldName, String fieldDefinitionSchema)
        {
            XElement schemaAnalysis = XElement.Parse(fieldDefinitionSchema);
            bool isHidden = ((schemaAnalysis.Element("Type").Attribute("Hidden") != null) ? true : false);
            string maxLength = ((schemaAnalysis.Element("Type").Attribute("MaxLength") == null) ? "100" : schemaAnalysis.Element("Type").Attribute("MaxLength").Value);
            return String.Format(SharepointListFieldsMassiveCreator.Properties.Resources.TextFieldBody,
                                 fieldName, isHidden.ToString().ToUpper(), maxLength);
        }

        private static string GetNumberFieldSchema(string fieldName, string fieldDefinitionSchema)
        {
            XElement schemaAnalysis = XElement.Parse(fieldDefinitionSchema);
            bool isHidden = ((schemaAnalysis.Element("Type").Attribute("Hidden") != null) ? true : false);
            string decimals = ((schemaAnalysis.Element("Type").Attribute("Decimals") == null) ? "0" : schemaAnalysis.Element("Type").Attribute("Decimals").Value);
            return String.Format(SharepointListFieldsMassiveCreator.Properties.Resources.NumberFieldBody,
                                 fieldName, isHidden.ToString().ToUpper(), decimals);
        }

        private static String GetCurrencyFieldSchema(String fieldName, String fieldDefinitionSchema)
        {
            XElement schemaAnalysis = XElement.Parse(fieldDefinitionSchema);
            bool isHidden = ((schemaAnalysis.Element("Type").Attribute("Hidden") != null) ? true : false);
            return String.Format(SharepointListFieldsMassiveCreator.Properties.Resources.CurrencyFieldBody,
                                 fieldName, isHidden.ToString().ToUpper());
        }

        private static String GetCalculatedFieldSchema(String fieldName, String fieldDefinitionSchema)
        {
            XElement schemaAnalysis = XElement.Parse(fieldDefinitionSchema);
            bool isHidden = ((schemaAnalysis.Element("Type").Attribute("Hidden") != null) ? true : false);
            String resultType = schemaAnalysis.Element("Type").Attribute("ResultType").Value;
            String description = schemaAnalysis.Element("Formula").Value.ToString().Replace("=", "");
            String formula = schemaAnalysis.Element("Formula").ToString();
            String fieldRefs = schemaAnalysis.Element("FieldRefs").ToString();
            return String.Format(SharepointListFieldsMassiveCreator.Properties.Resources.CalculatedFieldBody,
                                 fieldName, isHidden.ToString().ToUpper(), resultType, description, formula, fieldRefs);
        }

        private static string GetLinkFieldSchema(string fieldName, string fieldDefinitionSchema)
        {
            XElement schemaAnalysis = XElement.Parse(fieldDefinitionSchema);
            bool isHidden = ((schemaAnalysis.Element("Type").Attribute("Hidden") != null) ? true : false);
            return String.Format(SharepointListFieldsMassiveCreator.Properties.Resources.LinkFieldBody,
                                 fieldName, isHidden.ToString().ToUpper());
        }

        private static string GetPictureFieldSchema(string fieldName, string fieldDefinitionSchema)
        {
            XElement schemaAnalysis = XElement.Parse(fieldDefinitionSchema);
            bool isHidden = ((schemaAnalysis.Element("Type").Attribute("Hidden") != null) ? true : false);
            return String.Format(SharepointListFieldsMassiveCreator.Properties.Resources.PictureFieldBody,
                                 fieldName, isHidden.ToString().ToUpper());
        }

        private static string GetIntegerFieldSchema(string fieldName, string fieldDefinitionSchema)
        {
            // Integer numbers are not supported column data types in SharePoint and InfoPath. Use Number with Decimals="0" instead.
            XElement schemaAnalysis = XElement.Parse(fieldDefinitionSchema);
            bool isHidden = ((schemaAnalysis.Element("Type").Attribute("Hidden") != null) ? true : false);
            return String.Format(SharepointListFieldsMassiveCreator.Properties.Resources.IntegerFieldBody,
                                 fieldName, isHidden.ToString().ToUpper());
        }

    }
}
